package com.lagou.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.lagou.base.BaseServlet;
import com.lagou.pojo.Course;
import com.lagou.pojo.Course_Lesson;
import com.lagou.pojo.Course_Section;
import com.lagou.service.CourseContentService;
import com.lagou.service.impl.CourseContentServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet(name = "CourseContentServlet",urlPatterns = "/courseContent")
public class CourseContentServlet extends BaseServlet {


    /**
     * 名称: findSectionAndLessonByCourseId
     *     描述: 根据课程ID查询章节与课时信息
     */
    public void findSectionAndLessonByCourseId(HttpServletRequest req, HttpServletResponse resp){
        try {
            String course_id = req.getParameter("course_id");
            CourseContentService courseContentService = new CourseContentServiceImpl();
            List<Course_Section> list = courseContentService.findSectionAndLessonByCourseId(Integer.parseInt(course_id));
            String result = JSON.toJSONString(list);
            resp.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //根据课程ID回显课程信息
    public void findCourseById(HttpServletRequest req, HttpServletResponse resp){
        try {
            //1.获取参数
            String course_id = req.getParameter("course_id");

            //2.业务处理
            CourseContentService courseContentService = new CourseContentServiceImpl();
            Course course = courseContentService.findCourseByCourseId(Integer.parseInt(course_id));

            //3.转换json
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class,"id","course_name");

            String result = JSON.toJSONString(course, filter);
            resp.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 保存和修改章节信息
     * @param req
     * @param resp
     */
    public void saveOrUpdateSection(HttpServletRequest req, HttpServletResponse resp){
        try {
            //1.获取参数
            Map<String,Object> map = (Map)req.getAttribute("map");

            //2.创建Course_Section
            Course_Section section = new Course_Section();

            //3.使用BeanUtils工具类 将map中的数据封装到 section
            //BeanUtils.populate(section,map);
              BeanUtils.copyProperties(section,map.get("section"));

            //4.业务处理
            CourseContentService courseContentService = new CourseContentServiceImpl();
            String result = null;

            //判断是否携带ID
            if (section.getId()==0){
                result = courseContentService.saveSection(section);
            }else {
                result = courseContentService.updateSection(section);
            }

            //5.响应结果
            resp.getWriter().print(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //修改章节状态
    public void updateSectionStatus(HttpServletRequest req, HttpServletResponse resp){
        try {
            String id = req.getParameter("id");
            String status = req.getParameter("status");
            CourseContentService courseContentService = new CourseContentServiceImpl();
            String result = courseContentService.updateSectionStatus(Integer.parseInt(id), Integer.parseInt(status));
            resp.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //保存和修改课时信息
    public void saveOrUpdateLesson(HttpServletRequest req, HttpServletResponse resp){
        try {
            //1.获取参数
            Map<String,Object> map = (Map)req.getAttribute("map");

            //2.Course_Lesson
            Course_Lesson lesson = new Course_Lesson();

            //3.使用BeanUtils工具类 将map中的数据封装到 section
            BeanUtils.copyProperties(lesson,map.get("lesson"));


            //4.业务处理
            CourseContentService courseContentService = new CourseContentServiceImpl();
            String result = null;

            //判断是否携带ID
            if (lesson.getId()==0){
                result = courseContentService.saveLesson(lesson);
            }else {
                result = courseContentService.updateLesson(lesson);
            }

            //5.响应结果
            resp.getWriter().print(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
